package model

import (
	"goods/goodsrpc/goodsrpc"
	"gorm.io/gorm"
)

type Goods struct {
	gorm.Model
	Name    string  `gorm:"type:varchar(30);not null;commit:'商品名称'"`
	Price   float64 `gorm:"type:decimal(10,2);not null;commit:'商品价格'"`
	Stock   int     `gorm:"type:int;not null;commit:'商品库存'"`
	Image   string  `gorm:"type:varchar(225);not null;commit:'商品图片'"`
	ClassId int     `gorm:"type:int;not null;commit:'分类编号'"`
}

func (g *Goods) AddGoods(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}

func (g *Goods) FindGoodsByName(db *gorm.DB, name string) error {
	return db.Debug().Where("name=?", name).First(g).Error
}
func GoodList(db *gorm.DB, list []*goodsrpc.GoodList) []*goodsrpc.GoodList {
	db.Debug().Model(&Goods{}).
		Select("goods.id as good_id",
			"goods.name as good_name",
			"goods.price", "goods.stock",
			"goods.image", "classes.name as class_name",
			"goods.created_at").
		Joins("LEFT JOIN classes ON goods.class_id = classes.id").
		Find(&list)
	return list
}
